package xyz.wanwanwan.www.hrm.service.impl;

import org.springframework.stereotype.Service;
import xyz.wanwanwan.www.hrm.domain.CoursePromotion;
import xyz.wanwanwan.www.hrm.service.ISavePromotionToRedis;
import xyz.wanwanwan.www.hrm.util.RedisUtils;
import xyz.wanwanwan.www.hrm.util.SerializeUtil;

import java.util.ArrayList;
import java.util.List;

@Service
public class SavePromotionToRedisImpl implements ISavePromotionToRedis {
    public static final String TO_REDIS_COURSE_PROMOTION_KEY="to_redis_course_promotion_key";
    @Override
    public void savePromotion(CoursePromotion coursePromotion) {
        //将促销课程对象序列化
        byte[] serialize = SerializeUtil.serialize(coursePromotion);
        //存入redis
        RedisUtils.INSTANCE.hset(TO_REDIS_COURSE_PROMOTION_KEY.getBytes(), coursePromotion.getId().toString().getBytes(),
                serialize);
    }

    @Override
    public List<CoursePromotion> getListCoursePromotion() {
        List<byte[]> values = RedisUtils.INSTANCE.hvals(TO_REDIS_COURSE_PROMOTION_KEY.getBytes());
        List<CoursePromotion> result  = new ArrayList<>(values.size());	//用来封装结果的list
        values.forEach(value ->{
            result.add(((CoursePromotion)SerializeUtil.unserialize(value)));
        });
        return result;
    }

    @Override
    public byte[] getBuyCoursePromotion(Long id) {
        byte[] coursePromotion = RedisUtils.INSTANCE.hget(TO_REDIS_COURSE_PROMOTION_KEY.getBytes(),id.toString().getBytes());
        return coursePromotion;

    }

}
